package com.chuanye.jiguangimdemo.fragments;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


import com.chuanye.jiguangimdemo.R;

import com.chuanye.jiguangimdemo.activitys.FriendInfoActivity;
import com.chuanye.jiguangimdemo.activitys.GroupNotFriendActivity;
import com.chuanye.jiguangimdemo.activitys.SearchFriendDetailActivity;
import com.chuanye.jiguangimdemo.adapters.FriendRecommendAdapter;
import com.chuanye.jiguangimdemo.app.MyApplication;
import com.chuanye.jiguangimdemo.database.FriendRecommendEntry;
import com.chuanye.jiguangimdemo.database.UserEntry;
import com.chuanye.jiguangimdemo.entity.FriendInvitation;


import java.util.List;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.model.UserInfo;

/**
 * 好友验证页面
 */
public class FriendFragment extends BaseFragment {
    private static final String TAG = "FriendFragment";
    private Context mContext;

    private ListView mListView;

    private List<FriendRecommendEntry> mList;
    private FriendRecommendAdapter friendRecommendAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.fragment_friend_view,null);
       mContext = getActivity();

       initView(view);

        return view;
    }


    private void initView(View view) {
        mListView = view.findViewById(R.id.friend_recommend_list_view);

        initData();

        //好友验证列表条目点击事件
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                final FriendRecommendEntry entry = mList.get(position);
                final Intent intent;
                if (entry.state.equals(FriendInvitation.INVITED.getValue())) {//邀请
                    //1.没同意也没拒绝时--> 是否同意界面
                    intent = new Intent(mContext, SearchFriendDetailActivity.class);
                    intent.putExtra("reason", entry.reason);
                    intent.putExtra("position", position);
                    intent.putExtra(MyApplication.TARGET_ID, entry.username);
                    intent.putExtra(MyApplication.TARGET_APP_KEY, entry.appKey);
                    startActivityForResult(intent, 0);
                    //2.已经添加的 --> 好友详情
                } else if (entry.state.equals(FriendInvitation.ACCEPTED.getValue())) {
                    JMessageClient.getUserInfo(entry.username, new GetUserInfoCallback() {
                        @Override
                        public void gotResult(int i, String s, UserInfo userInfo) {
                            if (i == 0) {
                                Intent intent1 = new Intent();
                                if (userInfo.isFriend()) {
                                    intent1.setClass(mContext, FriendInfoActivity.class);
                                    intent1.putExtra("fromContact", true);
                                } else {
                                    intent1.setClass(mContext, GroupNotFriendActivity.class);
                                }
                                intent1.putExtra(MyApplication.TARGET_ID, entry.username);
                                intent1.putExtra(MyApplication.TARGET_APP_KEY, entry.appKey);
                                startActivityForResult(intent1, 0);
                            }
                        }
                    });

                    //3.自己拒绝、被对方拒绝、等待对方验证 --> 用户资料界面
                } else {
                    intent = new Intent(mContext, GroupNotFriendActivity.class);
                    intent.putExtra("reason", entry.reason);
                    intent.putExtra(MyApplication.TARGET_ID, entry.username);
                    intent.putExtra(MyApplication.TARGET_APP_KEY, entry.appKey);
                    startActivityForResult(intent, 0);
                }
            }
        });
        //好友验证列表条目长按事件
        mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                return false;
            }
        });

    }

    private void initData() {
        UserEntry user = MyApplication.getUserEntry();
        if (null != user) {
            mList = user.getRecommends();
            Log.e(TAG, "好友验证页面：好友验证列表长度："+mList.size());
            friendRecommendAdapter = new FriendRecommendAdapter(getActivity(), mList);
            mListView.setAdapter(friendRecommendAdapter);
        } else {
            Log.e(TAG, "好友验证页面：获取用户信息为空");
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case MyApplication.RESULT_BUTTON:
                int position = data.getIntExtra("position", -1);
                int btnState = data.getIntExtra("btn_state", -1);
                FriendRecommendEntry entry = mList.get(position);
                if (btnState == 2) {
                    entry.state = FriendInvitation.ACCEPTED.getValue();
                    entry.save();
                } else if (btnState == 1) {
                    entry.state = FriendInvitation.REFUSED.getValue();
                    entry.save();
                }
                break;
            default:
                break;
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        if (friendRecommendAdapter != null){
            friendRecommendAdapter.notifyDataSetChanged();
        }

    }

}
